import { isEmpty } from 'validator'
import storageKeys from '../config/storage.keys'

// 储存对象
class Storage {
    constructor () {
        /**
         * 定义储存等级
         * LEVEL_GLOBAL 全局储存
         * LEVEL_USER 储存在当前用户信息下
        */
        this.LEVEL_GLOBAL = 0
        this.LEVEL_USER = 1
    }

    /**
     * @description: 获取真实的key
     * @param {string} key key
     * @param {number} level 级别
     * @return {string} 
     */
    _getRealKey (key = '', level) {
        // 用户仓库
        if (level === this.LEVEL_USER) {
            const user = this.getObject(storageKeys.CONTEXT_USER)
            if (!user) {
				console.log('error: storage获取当前用户失败')
                throw new Error('storage获取当前用户失败')
            }
            return `user_storage_${key}_${user.id}`
        }
        // 全局
        return `global_storage_${key}`
    }

    /**
     * @description: 保存字符串
     * @param {string} key 键名
     * @param {string} value 键值
     * @param {number} [level] 可选，保存级别，默认全局
     * @return {void}
     */    
    saveString (key, value, level) {
        if (isEmpty(key)) throw new TypeError('key参数为空')
        if (value === undefined) throw new TypeError('value参数为空')
        key = this._getRealKey(key, level)
        try {
            uni.setStorageSync(key, value)
        } catch (e) {
            throw new Error(`saveString error: ${e}`)
        }
    }  

    saveUserString (key, value) {
        return this.saveString(key, value, this.LEVEL_USER)
    }
    
    /**
     * @description: 获取字符串
     * @param {string} key 键名
     * @param {number|level} [level] 可选，保存的级别，默认全局
     * @return {string} 
     */ 
    getString (key, level) {
        key = this._getRealKey(key, level)
        return uni.getStorageSync(key)
    }

    getUserString (key) {
		try {
			const result = this.getString(key, this.LEVEL_USER)
		} catch (err) {
			console.log({ err })
		}
        return result
    }

    /**
     * @description: 保存对象
     * @param {string} key 键名
     * @param {object} value 键值
     * @param {number|level} [level] 可选，保存的级别，默认全局
     * @return {void} 
     */
    saveObject (key, value, level) {
        try {
            value = JSON.stringify(value)
        } catch (e) {
            throw new Error(`saveObject json stringify error: ${e}`)
        }
        this.saveString(key, value, level)
    }

    saveUserObject (key, value) {
        this.saveObject(key, value, this.LEVEL_USER)
    }

    /**
     * @description: 获取对象
     * @param {string} key 键名
     * @param {number|level} [level] 可选，保存的级别，默认全局
     * @return {object || null} 
     */    
    getObject (key, level) {
        const str = this.getString(key, level)
        if (str) {
            try {
                return JSON.parse(str)
            } catch {
                return null
            }
        } else {
            return null
        }
    }

    getUserObj (key) {
        return this.getObject(key, this.LEVEL_USER)
    }

    /**
     * @description: 移除
     * @param {string} key 键名
     * @param {number|level} level 可选，保存的级别，默认全局
     * @return {void} 
     */    
    remove (key, level) {
        key = this._getRealKey(key, level)
        uni.removeStorageSync(key);
    }

    removeUserKey (key) {
        this.remove(key, this.LEVEL_USER)
    }

    /**
     * 清空
    */
    clear () {
        uni.clearStorageSync();
    }
}

export default new Storage()